<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>搜狐新闻全文搜索引擎</title>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<link href="css/common.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/index.css" rel="stylesheet" type="text/css" media="screen" />
<script type='text/javascript' src="${pageContext.request.contextPath}/dwr/engine.js"></script>
<script type='text/javascript' src="${pageContext.request.contextPath}/dwr/util.js"></script>
<script type='text/javascript' src="${pageContext.request.contextPath}/dwr/interface/searchService.js"></script>
<script language="javascript">

var request;
var minpage;
var maxpage;
var startindex;
var hasnext;

function doSearch(type)
{

	var result = document.getElementById("result");
	var pagingdiv = document.getElementById('paging');
	result.innerHTML="";
	pagingdiv.innerHTML = "";

	if (type != 'paging') 
	{
		var startindexinput = document.getElementById('startindex');
		startindexinput.value = "1";
	}	
	
	request = {startindex:1, query:""};
	DWRUtil.getValues(request);

	searchService.getSearchResults(request, fillPage);
}

function fillPage(data)
{
	//alert(DWRUtil.toDescriptiveString(data, 2));// 调试获得的记录
	var list = data.results;
	var resultdiv = document.getElementById('result');
	var pagingdiv = document.getElementById('paging');
	var infoDiv=document.getElementById("information");
	resultdiv.innerHTML = "";
	pagingdiv.innerHTML = "";
	infoDiv.innerHTML="";
	if (list.length == 0) 
	{
		resultdiv.innerHTML = "<span>对不起，没有查到您需要的信息...</span>";
		return;
	}
	if(data.usedTime<=0)
	{
		infoDiv.innerHTML="找到约 "+data.length+" 条结果 （用时 <1ms） ";
	}else
	{
		infoDiv.innerHTML="找到约 "+data.length+" 条结果 （用时 "+data.usedTime+"秒） ";
	}
	
	for(var i=0; i<list.length; i++) 
	{
		var ele = document.createElement('div');
		ele.setAttribute('id','info' + list[i]);
		ele.innerHTML = "<span>loading..</span>";
		resultdiv.appendChild(ele);
		//alert(list[i]);
		searchService.getSearchResultById(list[i], fillDetailResult);
	}
	
	minpage = data.minpage;
	maxpage = data.maxpage;
	startindex = data.startindex;
	hasnext = data.hasnext;
	//12日开始的检查点
	if (minpage != 1) 
	{
		var link = document.createElement('a');
		link.setAttribute("href","javascript:paging('" + ((minpage-11)*10+1) + "')");
		link.innerHTML = "前10页<<";
		pagingdiv.appendChild(link);
	}
	
	for (var j=minpage; j <=maxpage; j++)
	{
		if ((j-1)*10+1 != startindex)
		{
			var link = document.createElement('a');
			link.setAttribute("href","javascript:paging('" + ((j-1)*10+1) + "')");
			link.innerHTML = "第" + j + "页";
			pagingdiv.appendChild(link);
		}
		else 
		{
			pagingdiv.innerHTML += ("第" + j + "页");
		}
	}
	
	if (hasnext == 1) 
	{
		var link = document.createElement('a');
		link.setAttribute("href","javascript:paging('" + (maxpage*10+1) + "')");
		link.innerHTML = ">>后10页";
		pagingdiv.appendChild(link);
	}
	
}

function fillDetailResult(record) 
{
	//alert(DWRUtil.toDescriptiveString(record, 2));// 调试获得的记录
	var s=document.getElementById('query').value;
	var result = document.getElementById('info' + record.newsId);
	result.innerHTML="";
	result.innerHTML="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"60%\"  style=\"margin-left:18px;\"> <tr><td align=\"left\" style=\"font-size: 100%;line-height: 25px;line-height: 115%;\"><h3><a href=\""+record.newsUrl+"\" target=\"_blank\" style=\"text-decoration: underline;font-size: 16px;\"> <em>"
	                  + record.newsTitle + 
	                  "</em>-["+record.newsSource+"]</a></h3></td></tr>"+
	                  "<tr><td align=\"left\" style=\"font-size: 13px;\">"+substr(record.newsCotent,300)+"</td></tr>"+
	                    "<tr><td align=\"left\"><span style=\"font-size:12px; color:green\">"+record.newsUrl+"-"+record.newsAddtime+"</span>  --<a href=\"d?id="+record.newsId +"&s="+s+"\" target=\"_blank\" style=\"COLOR: #666;font-size: 100%;color: #666;\">网页快照</a></td></tr>"+
	                  "</table>";
	 
}

function paging(newindex) 
{
	document.getElementById('startindex').value = newindex;
	doSearch('paging');
}

function handlekey()
{
	if (document.getElementById('query').value == '')
		return;
	var intkey = -1;
	if(window.event) 
	{
		intKey = event.keyCode;
		if(intKey == 13)
		{
			doSearch('');
		}
	}
}

function substr(str, len)
{
    if(!str || !len) { return ''; }
    var a = 0;
    var i = 0;
    var temp = '';
    for (i=0;i<str.length;i++)
    {
        if (str.charCodeAt(i)>255)  
        {
             a+=2;
        }
        else
        {
             a++;
        }
        if(a > len) { return temp; }
         temp += str.charAt(i);
    }
    return str;
}


</script>

</head>
<body>
<input type="hidden" name="startindex" id="startindex" value="1">
<!-- 
<div id = 'top'>
    <div id="left">
        <span>首页</span>
        <a href="#" target = _parent>新闻</a>
        <a href="#" target = _parent>音乐</a>
        <a href="#" target = _parent>翻译</a>
        <a href="#" target = _parent>黄页</a>
        <a href="#" target = _parent>知道</a>
    </div>
    <div id="right">
        
            <a href="/login/">登录</a> | <a href="/login/register/">注册</a>
    </div>
</div>
 -->
<div id = 'index_logo'>
<IMG id=logo title="搜搜搜索" border=0 alt="搜搜搜索" src="#"><br>
</div>

<!-- 这是搜索栏 -->
<div id="index_search">
	<input size="50" type="text" name="query" id="query" value="小人物"  class = 'input'  onkeyup="handlekey()">
	<input type="button" value="Search"  id="search"  class = 'button' onclick="javascript:doSearch('')">
</div>

<hr>
<div id="information"></div>
<!-- 这是结果栏 -->
<div id="result">

</div>

<hr>

<!-- 这是分页栏 -->
<div id="paging">

</div>

<!-- 这是Footer -->
<div id="footer">	
	@2011&nbsp&nbsp东北师范大学 理想信息技术研究院 版权所有				
</div>
</body>
</html>